package org.eenie.kdassistant.float_win;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.eenie.kdassistant.R;
import org.eenie.kdassistant.ui.ExpressAdapter;
import org.eenie.kdassistant.ui.ExpressEntity;
import org.eenie.kdassistant.utils.ApplicationUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eenie on 2017/7/31.
 * Email: eeniework@gmail.com
 * Des:
 */

public class FloatWindowSmallView extends LinearLayout {
    /**
     * 记录小悬浮窗的宽度
     */
    public static int viewWidth;

    /**
     * 记录小悬浮窗的高度
     */
    public static int viewHeight;

    /**
     * 记录系统状态栏的高度
     */
    private static int statusBarHeight;

    /**
     * 用于更新小悬浮窗的位置
     */
    private WindowManager windowManager;

    /**
     * 小悬浮窗的参数
     */
    private WindowManager.LayoutParams mParams;

    /**
     * 记录当前手指位置在屏幕上的横坐标值
     */
    private float xInScreen;

    /**
     * 记录当前手指位置在屏幕上的纵坐标值
     */
    private float yInScreen;

    /**
     * 记录手指按下时在屏幕上的横坐标的值
     */
    private float xDownInScreen;

    /**
     * 记录手指按下时在屏幕上的纵坐标的值
     */
    private float yDownInScreen;

    /**
     * 记录手指按下时在小悬浮窗的View上的横坐标的值
     */
    private float xInView;

    /**
     * 记录手指按下时在小悬浮窗的View上的纵坐标的值
     */
    private float yInView;


    private Context mContext;


    private ListView mExpressList;

    private ExpressAdapter mExpressAdapter;

    private TextView tv;

    List<String> numList = new ArrayList<>();


    List<ExpressEntity> mExpressEntities = new ArrayList<>();


    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            // TODO: 2017/8/7 其他APP发送的单号广播


//            tv.setText(String.valueOf(mExpressEntities.size()));
//
//            tv.setText(String.valueOf(mExpressEntities.size()));
//
//            if (!numList.contains(intent.getStringExtra("express"))) {
//
//                numList.add(intent.getStringExtra("express"));
//                mExpressEntities.add(new ExpressEntity(intent.getStringExtra("src"), intent.getStringExtra("express")));
//                mExpressAdapter.notifyDataSetChanged();
//            } else {
//                System.out.println("重复");
//            }


        }
    };


    public FloatWindowSmallView(Context context) {
        super(context);
        mContext = context;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater.from(context).inflate(R.layout.float_win_layout, this);
        View view = findViewById(R.id.small_window_layout);

        mExpressList = (ListView) findViewById(R.id.list_express);
        mExpressList.setAdapter(mExpressAdapter = new ExpressAdapter(mContext, mExpressEntities));

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.wrbug.xposeddemo.express");

        mContext.registerReceiver(mBroadcastReceiver, intentFilter);

        findViewById(R.id.tv_xz).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.wrbug.xposeddemo.xz");
                intent.putExtra("method", 0);
                mContext.sendBroadcast(intent);
            }
        });
        findViewById(R.id.tv_yd).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.wrbug.xposeddemo.st");
                intent.putExtra("method", 2);

                mContext.sendBroadcast(intent);
            }
        });
        findViewById(R.id.tv_sf).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("com.wrbug.xposeddemo.zzt");
                intent.putExtra("method", 1);
                mContext.sendBroadcast(intent);
            }
        });


        tv = (TextView) findViewById(R.id.tv_scala);
        tv.setTag(0);


        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv.getTag().equals(1)) {
                    tv.setTag(0);
                    mExpressList.setVisibility(GONE);
                } else {
                    tv.setTag(1);
                    mExpressList.setVisibility(VISIBLE);
                }
            }
        });


        findViewById(R.id.tv_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ApplicationUtil.back(mContext);

//                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });

        viewWidth = view.getLayoutParams().width;
        viewHeight = view.getLayoutParams().height;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
                xInView = event.getX();
                yInView = event.getY();
                xDownInScreen = event.getRawX();
                yDownInScreen = event.getRawY() - getStatusBarHeight();
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - getStatusBarHeight();
                break;
            case MotionEvent.ACTION_MOVE:
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - getStatusBarHeight();
                // 手指移动的时候更新小悬浮窗的位置
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:

                break;
            default:
                break;
        }
        return true;
    }

    /**
     * 将小悬浮窗的参数传入，用于更新小悬浮窗的位置。
     *
     * @param params 小悬浮窗的参数
     */
    public void setParams(WindowManager.LayoutParams params) {
        mParams = params;
    }

    /**
     * 更新小悬浮窗在屏幕中的位置。
     */
    private void updateViewPosition() {
        mParams.x = (int) (xInScreen - xInView);
        mParams.y = (int) (yInScreen - yInView);
        windowManager.updateViewLayout(this, mParams);
    }


    /**
     * 用于获取状态栏的高度。
     *
     * @return 返回状态栏高度的像素值。
     */
    private int getStatusBarHeight() {
        if (statusBarHeight == 0) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = (Integer) field.get(o);
                statusBarHeight = getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusBarHeight;
    }
}
